import { Component } from '@angular/core';
import { IonicPage,NavController, NavParams, ToastController} from 'ionic-angular';
// import { InAppBrowser } from '@ionic-native/in-app-browser';
import {ApplicantService} from "../../service/http-service/applicant.service";
import {GetVideoDetailService} from "../../service/http-service/getVideoDetail.service";
import {ValidateUtils} from "../../util/validate-utils";

/**
 * Generated class for the MystatePage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */
@IonicPage()
@Component({
  selector: 'page-mystate',
  templateUrl: 'mystate.html',
})
export class MystatePage {
  mystateUrl: string;
  myComment:string;
  videoID:string;
  toastCtrl:ToastController;
  score: any = {
    star: 0,
    starMap: [
      '',
      '',
      '',
      '',
      '',
    ]
  }
  chooseStar(e){
    let star = parseInt(e.target.dataset.index);
    this.score.star = star;
    if(star&&star!=undefined){
      this.score.star = star;
    }else {
      this.score.star = 0;
    }
  }


  constructor(public validate:ValidateUtils,public navCtrl: NavController, public navParams: NavParams,public getVideoDetailservice:GetVideoDetailService,public  applicationService:ApplicantService,public toastService:ToastController) {
    this.videoID=navParams.get("Id");
    console.log(this.videoID)
    this.toastCtrl=toastService;
  }
  // Load() {
  //   console.log('ionViewDidLoad MystatePage');
  // }

  // openNew(){
  //
  //   localStorage.setItem("mystateUrl", window.location.href);
  //   const browser =this.iab.create('http://5d95cg.natappfree.cc/han/account/wxAuth', '_self', {location: "yes"});
  //   //  do{
  //   //   this.MyState=localStorage.getItem("MyState")
  //   //  }
  //   //  while(this.MyState="0")
  //   //  browser.close();
  //    console.log(JSON.parse(localStorage.getItem("options")))
  // }
  goComment(commend){
    this.getVideoDetailservice.postCommend(this.videoID,commend).subscribe(resp=>{
      if (resp.data!==null&& resp.code==200){
        console.log("请求成功");
        console.log("goComment\n"+resp.data);
        this.navCtrl.pop();
      }else if(resp.data!==null &&resp.code==1401){
        console.log("token 非法");
        this.applicationService.freshTokenn();
      }
    },error2 => {
      console.log("请求出错");
      console.log(error2);
    });
  }
  goScore(score:string){
    this.getVideoDetailservice.putScore(this.videoID,score).subscribe(resp=>{
      if (resp.data!==null&& resp.code==200){
        if(!resp.data){
          console.log("goScore\n"+resp.data);
        }
      }else if(resp.data!==null &&resp.code==1401){
        this.applicationService.freshTokenn();
      }
    },error2 => {
      console.log(error2);
    });
  }

  comment(){
    // var text= document.querySelector('#commentText');

    if(null==this.myComment)
      this.validate.showtoast("您还没有填写评语哦！",this.toastCtrl,'middle');
    else if (this.score.star==0)
      this.validate.showtoast("请给视频打个分吧^_^",this.toastCtrl,'middle');
    else {
      console.log("textarea"+this.myComment);
      this.goComment(this.myComment);
      this.goScore(this.score.star);

    }
  }
}
